package com.herui.iot.ui.mime;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.herui.sdyu_lib.utils.ToastUtil;
import com.herui.sport_care.R;
import com.herui.sport_care.ui.BaseSportActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class UserChangePasswordActivity extends BaseSportActivity {

    private UserPresenter userPresenter;

    @BindView(R.id.et_old_password)
    EditText OldPassword;
    @BindView(R.id.et_new_one_password)
    EditText newOnePassword;
    @BindView(R.id.et_new_two_password)
    EditText newTwoPasswod;
    @BindView(R.id.view_confirm_password)
    LinearLayout confirmPassword;
    @BindView(R.id.view_confirm_two_password)
    LinearLayout confirmTwoPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("修改密码");
        userPresenter = new UserPresenter(userView);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_user_change_password;
    }

    @Override
    public void initParam() {

//        newOnePassword.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
////                System.out.println("文本框输入："+password.getText().toString());
//                String oldPass = OldPassword.getText().toString();
//                String newOnePass = newOnePassword.getText().toString();
//                String newTwoPass = newTwoPasswod.getText().toString();
//                if(newOnePass.equals(newTwoPass)){
//                    confirmTwoPassword.setVisibility(View.INVISIBLE);
//                }else{
//                    confirmTwoPassword.setVisibility(View.VISIBLE);
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//
//            }
//        });

        newTwoPasswod.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//                System.out.println("文本框输入："+password.getText().toString());
                String oldPass = OldPassword.getText().toString();
                String newOnePass = newOnePassword.getText().toString();
                String newTwoPass = newTwoPasswod.getText().toString();
                if(newOnePass.equals(newTwoPass)){
                    confirmTwoPassword.setVisibility(View.INVISIBLE);
                }else{
                    confirmTwoPassword.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        OldPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    confirmPassword.setVisibility(View.INVISIBLE);
                    Log.i("OldPassword","获取焦点");
                }
            }
        });
    }

    @OnClick({R.id.btn_confirm,R.id.btn_cancel})
    public void onViewClick(View view) {
        switch (view.getId()){
            case R.id.btn_confirm:
                String oldPass = OldPassword.getText().toString();
                String newOnePass = newOnePassword.getText().toString();
                String newTwoPass = newTwoPasswod.getText().toString();
                if(newOnePass.equals(newTwoPass)){
                    confirmTwoPassword.setVisibility(View.INVISIBLE);
                    userPresenter.changePassword(newOnePass,oldPass);
                }else{
                    confirmTwoPassword.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.btn_cancel:
                onBackPressed();
                break;
            default:
                break;
        }

    }

    UserPresenter.UserView userView = new UserPresenter.UserView() {
        @Override
        public void onChangePassword(boolean flag) {
            if(flag){
                confirmPassword.setVisibility(View.INVISIBLE);
                ToastUtil.showMessage("密码修改成功");
                onBackPressed();
            }else{
                confirmPassword.setVisibility(View.VISIBLE);
                ToastUtil.showMessage("密码修改失败");
            }
        }
    };
}